﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LYQ.BLL;
using WebManager;

namespace BaoJian.WebManager.Manager.KeyWord
{
    public partial class List : System.Web.UI.Page
    {
        private string columnId;
        private LYQ.BLL.Keyword bll = new Keyword();
        protected void Page_Load(object sender, EventArgs e)
        {
            columnId = HttpContext.Current.Request.QueryString["OID"];
            if (columnId == null)
                return;
            if (!Page.IsPostBack)
            {
                ViewState["KLevel"] = "KLevel";
                ViewState["OrderDire"] = "Desc";

                BindData();
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();

        }

        private void BindData()
        {
            DataView dataView = bll.GetAllList().Tables[0].DefaultView;
            GridView1.DataSource = dataView;
            string sort = (string)ViewState["KLevel"] + " " + (string)ViewState["OrderDire"];
            dataView.Sort = sort;
            GridView1.DataKeyNames = new string[] { "SID" };//主键
            GridView1.DataBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string SID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
            string klevel = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
            LYQ.Model.Keyword model = bll.GetModel(Convert.ToInt32(SID));
            model.KLevel = Convert.ToInt32(klevel);
            model.UDate = DateTime.Now;
            bll.Update(model);
            GridView1.EditIndex = -1; 
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void New_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/KeyWord/NewKey.aspx?ColumnID" + columnId;
            Page.Response.Redirect(url);
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            string sel = HttpContext.Current.Request["selectCB"];
            if (string.IsNullOrEmpty(sel))
            {
                return;
            }
            HttpContext.Current.Session["EditKeys"] = sel;
             string url = URLUtility.UrlBase + @"/WebManager/manager/KeyWord/EditKey.aspx?ColumnID" + columnId;
            Page.Response.Redirect(url);
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            string sel = HttpContext.Current.Request["selectCB"];
            if (string.IsNullOrEmpty(sel))
            {
                return;
            }
            string[] keys = sel.Split(',');
            foreach (string s in keys)
            {
                int sid = Convert.ToInt32(s);
                bll.Delete(sid);
            }
            BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string SID = GridView1.DataKeys[e.RowIndex].Value.ToString();
            bll.Delete(Convert.ToInt32(SID));
            BindData();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            if (ViewState["KLevel"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["KLevel"] = e.SortExpression;
            }
            BindData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int i;
            //执行循环，保证每条数据都可以更新
            for (i = 0; i < GridView1.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }
    }
}
